/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.tools;

import java.awt.geom.Point2D;
import java.util.HashSet;
import java.util.Set;

import dgbgui.command.Command;
import dgbgui.command.CommandStack;
import dgbgui.command.PasteCommand;
import dgbgui.modeldocument.ModelDocument;
import dgbgui.modeldocument.Query;

public class PasteTool implements Tool {

    public void execute() {
        // Take the current clip board and paste it. But we're going
        // to paste cloned copies of the queries to the model document,
        // and we're creating the clones here, so each past command can 
        // be undone
        Set<Query> clipBoard = ModelDocument.getInstance().getClipBoard();
        assert ( clipBoard.size() > 0 );
        
        Set<Query> clones = new HashSet<Query>();
        for ( Query query : clipBoard ) {
            Query clonedQuery = (Query)query.clone();
            clones.add(clonedQuery);
            // TODO: Because clones also have the same state, they will show
            // up in the exact same position :(
            // As a workaround, add an offeset
            // Also related to Ticekt #33
            clonedQuery.getState().setPosition(new Point2D.Double(
                    clonedQuery.getState().getPosition().getX() + 30,
                    clonedQuery.getState().getPosition().getY() + 30));
                    
        }

        Command pastCommand = new PasteCommand(clones);
        CommandStack.getInstance().execute(pastCommand);

    }

}
